﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace EntitiesGenerator.Dtos
{
    /// <summary>
    /// 上一次使用的一些配置数据
    /// </summary>
    public class LastDataConfiguration
    {
        private static string _lastDataFile;
        static LastDataConfiguration()
        {
            var basepath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            _lastDataFile = Path.Combine(basepath, "lastdata.json");

            if (File.Exists(_lastDataFile))
            {

                string content = File.ReadAllText(_lastDataFile, Encoding.UTF8);
                Instance = JsonConvert.DeserializeObject<LastDataConfiguration>(content);

                //存在隐患bug
                //var keyValue = File.ReadAllLines(_lastDataFile)
                //       .Where(r => !r.StartsWith("--") && !r.IsNullOrWhiteSpace())
                //       .ToDictionary(r => r.Split(':')[0], r => r.Split(':')[1]);


                //Instance.ConnectionString = keyValue["ConnectionString"];
                //Instance.NameSpace = keyValue["NameSpace"];
                //Instance.EntitiesSavePath = keyValue["EntitiesSavePath"];
                //Instance.InheritClassName = keyValue["InheritClassName"];
                //Instance.NugetUrl = keyValue["NugetUrl"];
                //Instance.NugetKey = keyValue["NugetKey"];
            }
            else
                Instance = new LastDataConfiguration();

        }
        private LastDataConfiguration() { }

        public static LastDataConfiguration Instance { get; private set; }

        public string ConnectionString { get; set; }
        public string NameSpace { get; set; }

        public string EntitiesSavePath { get; set; }

        public string InheritClassName { get; set; }

        public string NugetUrl { get; set; }

        public string NugetKey { get; set; }

        public void Save()
        {
            //StringBuilder sb = new StringBuilder();
            //sb.AppendLine("ConnectionString:" + ConnectionString);
            //sb.AppendLine("NameSpace:" + NameSpace);
            //sb.AppendLine("EntitiesSavePath:" + EntitiesSavePath);
            //sb.AppendLine("InheritClassName:" + InheritClassName);
            //sb.AppendLine("NugetUrl:" + NugetUrl);
            //sb.AppendLine("NugetKey:" + NugetKey);

            string content = JsonConvert.SerializeObject(Instance);
            File.WriteAllText(_lastDataFile, content, Encoding.UTF8);
        }

    }
}
